package com.qf.team2.netdisc.controller;

import com.qf.team2.netdisc.common.base.BaseController;
import com.qf.team2.netdisc.common.pojo.entity.Document;
import com.qf.team2.netdisc.common.pojo.vo.RespResultVO;
import com.qf.team2.netdisc.service.ShowDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author
 */
@Controller
@RequestMapping("show")
public class ShowDocumentController extends BaseController<Object> {

    @Autowired
    private ShowDocumentService showDocumentService;

    @ResponseBody
    @RequestMapping(value = "showDocumentByName", method = RequestMethod.POST)
    public RespResultVO<Object> showDocumentList(HttpServletRequest req) {
        Cookie[] cookies = req.getCookies();
        Integer userId = null;
        for (Cookie cookie : cookies) {
            if ("userId".equals(cookie.getName())) {
                userId = Integer.valueOf(cookie.getValue());
            }
        }

        List<Document> documentList = showDocumentService.getDocumentListByIdAndName(userId);

        return successResp(documentList, "http://localhost:8080/netdisc/views/view-api/index.html?documentId=");
    }

    @ResponseBody
    @RequestMapping(value = "showDocumentByMD5", method = RequestMethod.POST)
    public RespResultVO<Object> showDocumentListByMD5(String md5) {
        List<Document> documentList = showDocumentService.getDocumentListByMD5(md5);

        return successResp(documentList, "http://localhost:8080/netdisc/views/view-api/index.html?documentId=");
    }

    @ResponseBody
    @RequestMapping(value = "showDocumentById", method = RequestMethod.POST)
    public RespResultVO<Object> showDocumentListById(Integer documentId) {

        List<Document> documentList = showDocumentService.getDocumentListByDocumentId(documentId);

        return successResp(documentList, "http://localhost:8080/netdisc/views/view-api/index.html?documentId=");
    }
}
